package com.fenghongzhang.day008;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private Button write;
    private Button read;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        write = (Button) findViewById(R.id.write);
        read = (Button) findViewById(R.id.read);

        read.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences abc = getSharedPreferences("abc", MODE_PRIVATE);
                //
                String name = abc.getString("name","大明");
                int age = abc.getInt("age", 14);
                Toast.makeText(MainActivity.this, name+age, Toast.LENGTH_SHORT).show();
            }
        });

        write.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //取到sp对象
                /**
                 * 文件名 有就打开,没有就创建
                 * 打开文件的方式
                 */
                SharedPreferences sp = getSharedPreferences("abc", MODE_PRIVATE);
                SharedPreferences.Editor edit = sp.edit();
                edit.putBoolean("isLogin",true);
                edit.putString("name","明世隐");
                edit.putInt("age",16);
                edit.putFloat("money",0.11f);
                edit.putLong("xinsuan",1000l);
                edit.apply();

            }
        });

    }
}
